﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using anpr.Models;
using System.Data.Objects;
using System.Web.Profile;
namespace anpr.Controllers
{ 
    
    public class OfficerController : Controller
    {
        private ANPREntities db = new ANPREntities();

        //
        // GET: /Officer/

        public ViewResult Index()
        {
            return View(db.officers.ToList());
        }

        //
        // GET: /Officer/Details/5

        public ViewResult Details(int id)
        {
            Profile officer = db.officers.Find(id);
            return View(officer);
        }

        //
        // GET: /Officer/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Officer/Create

        [HttpPost]
        public ActionResult Create(Profile officer)
        {
            if (ModelState.IsValid)
            {
               
                //officer.createDate = System.DateTime.Now;
                db.officers.Add(officer);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(officer);
        }
        
        //
        // GET: /Officer/Edit/5
 
        public ActionResult Edit(int? id)
        {
            Profile officer = db.officers.Find(id);
            return View(officer);
        }

        //
        // POST: /Officer/Edit/5

        [HttpPost]
        public ActionResult Edit(Profile officer)
        {
            //try
            //{
                if (ModelState.IsValid)
                {
                    //System.Diagnostics.Debug.WriteLine("hello");
                    db.Entry(officer).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            //}
            //catch (DataException)
            //{
                //log the error (add a var name after DataException)
            //    ModelState.AddModelError("", "unable to save changes. Try again later");
            //}
            return View(officer);
        }

        //
        // GET: /Officer/Delete/5
 
        public ActionResult Delete(int id)
        {
            Profile officer = db.officers.Find(id);
            return View(officer);
        }

        //
        // POST: /Officer/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Profile officer = db.officers.Find(id);
            db.officers.Remove(officer);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        
    }
}